package com.jiac.coderunner.util;

import com.jiac.coderunner.entity.FieldInfo;
import org.springframework.util.StringUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DBInfo {



    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(DBCache.getDbUrl(),DBCache.userName,DBCache.passWord);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static DatabaseMetaData getMetaData(){
        DatabaseMetaData metaData = null;
        try {
            Connection connection = getConnection();
            metaData = connection.getMetaData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return metaData;
    }

    /**
     * 获取当前用户所有表
     * @return
     */
    public static List<Map<String,Object>> getTables(){
        List<Map<String,Object>> tables = new ArrayList<Map<String,Object>>();
        try {
            String sql = "select t.* from information_schema.tables t where t.table_schema=?";
            Connection connection = getConnection();
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setObject(1,DBCache.dbName);
            ResultSet resultSet = pst.executeQuery();
            while (resultSet.next()){
                Map<String,Object> map = new HashMap<>();
                map.put("tableName",resultSet.getString("TABLE_NAME"));
                map.put("tableDesc",resultSet.getString("TABLE_COMMENT"));
                tables.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tables;
    }

    /**
     * 获取表字段信息
     * @param tableName
     * @return
     */
    public static List<FieldInfo> getTableCloumns(String tableName){
        List<FieldInfo> list = new ArrayList<FieldInfo>();
        Connection connection = getConnection();
        if (connection==null) {
            return list;
        }
        try{
            Statement stmt = connection.createStatement();
            String sql = "select column_name, data_type, column_key, is_nullable, column_comment from information_schema.columns where table_name='" + tableName + "'and table_schema='" + DBCache.dbName + "'";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()){
                FieldInfo fieldInfo = new FieldInfo();
                fieldInfo.setColumnName(rs.getString("column_name"));
                fieldInfo.setDataType(rs.getString("data_type"));
                fieldInfo.setIsKey("PRI".equals(rs.getString("column_key"))?true:false);
                fieldInfo.setNullAble("YES".equals(rs.getString("is_nullable"))?true:false);
                fieldInfo.setComment(rs.getString("column_comment"));
                list.add(fieldInfo);
            }
        }
        catch (SQLException e){
            e.printStackTrace();
        }finally{
            if(null != connection){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }
}
